/**
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-12-07 14:44:46
 * Copyright (c) 2015- joysuch-版权所有
 */

package com.joysuch.wwyt.bp.controller;

import java.util.List;

import javax.validation.Valid;

import com.joysuch.wwyt.bp.bean.BpDeviceFacilityCollectionBean;
import com.joysuch.wwyt.bp.bean.BpMainMaterialEditBean;
import com.joysuch.wwyt.bp.bean.BpMainMaterialListBean;
import com.joysuch.wwyt.bp.bean.BpMainMaterialListBean.Search;
import com.joysuch.wwyt.bp.bean.contractor.BpContractorBean;
import com.joysuch.wwyt.bp.service.BpMainMaterialService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/bp/main-material")
@Api(tags = "/bp/main-material 生产原料")
public class BpMainMaterialController {

    private static final Logger logger = LoggerFactory.getLogger(BpMainMaterialController.class);

    @Autowired
    private BpMainMaterialService bpMainMaterialService;

    @PostMapping("add")
    @ApiOperation(value = "生产原料-新增", response = BpMainMaterialEditBean.class)
    public Object add(@RequestBody @Valid BpMainMaterialEditBean entity) throws Exception {
        entity.setType("生产原料");
        bpMainMaterialService.add(entity);
        return ResultBean.success("");
    }

    @PostMapping("save")
    @ApiOperation(value = "中间产品-新增", response = BpMainMaterialEditBean.class)
    public Object save(@RequestBody @Valid BpMainMaterialEditBean entity) throws Exception {
        entity.setType("中间产品");
        bpMainMaterialService.add(entity);
        return ResultBean.success("");
    }

    @PostMapping("/finalAdd")
    @ApiOperation(value = "最终产品-新增", response = BpMainMaterialEditBean.class)
    public Object finalAdd(@RequestBody @Valid BpMainMaterialEditBean entity) throws Exception {
        entity.setType("最终产品");
        bpMainMaterialService.add(entity);
        return ResultBean.success("");
    }

    @PostMapping("update")
    @ApiOperation(value = "生产原料-修改", response = BpMainMaterialEditBean.class)
    public Object update(@RequestBody @Valid BpMainMaterialEditBean entity) throws Exception {
        if (entity.getId() == null) {
            return ResultBean.fail(102, "未指定主键");
        }
        entity.setType("生产原料");
        bpMainMaterialService.update(entity);
        return ResultBean.success("");
    }

    @PostMapping("edit")
    @ApiOperation(value = "中间产品-修改", response = BpMainMaterialEditBean.class)
    public Object edit(@RequestBody @Valid BpMainMaterialEditBean entity) throws Exception {
        if (entity.getId() == null) {
            return ResultBean.fail(102, "未指定主键");
        }
        entity.setType("中间产品");
        bpMainMaterialService.update(entity);
        return ResultBean.success("");
    }

    @PostMapping("change")
    @ApiOperation(value = "最终产品-修改", response = BpMainMaterialEditBean.class)
    public Object change(@RequestBody @Valid BpMainMaterialEditBean entity) throws Exception {
        if (entity.getId() == null) {
            return ResultBean.fail(102, "未指定主键");
        }
        entity.setType("最终产品");
        bpMainMaterialService.update(entity);
        return ResultBean.success("");
    }

    @GetMapping("page")
    public Object page(Integer page, Integer size, Search condition) {
        try {
            condition.setType("生产原料");
            Page<BpMainMaterialListBean> data = bpMainMaterialService.findByPage(QPageRequest.of(page, size),
                    condition);
            return ResultBean.pageData(data.getContent(), data.getTotalElements());
        } catch (Exception e) {
            logger.error("分页查询失败", e);
            return ResultBean.fail(101, "分页查询失败");
        }
    }

    @GetMapping("/list")
    public Object list(Integer page, Integer size, Search condition) {
        try {
            condition.setType("中间产品");
            Page<BpMainMaterialListBean> data = bpMainMaterialService.findByPage(QPageRequest.of(page, size),
                    condition);
            return ResultBean.pageData(data.getContent(), data.getTotalElements());
        } catch (Exception e) {
            logger.error("分页查询失败", e);
            return ResultBean.fail(101, "分页查询失败");
        }
    }

    @GetMapping("/findAll")
    public Object findAll(Integer page, Integer size, Search condition) {
        try {
            condition.setType("最终产品");
            Page<BpMainMaterialListBean> data = bpMainMaterialService.findByPage(QPageRequest.of(page, size),
                    condition);
            return ResultBean.pageData(data.getContent(), data.getTotalElements());
        } catch (Exception e) {
            logger.error("分页查询失败", e);
            return ResultBean.fail(101, "分页查询失败");
        }
    }

    @PostMapping("delete")
    public Object delete(Long id) {
        try {
            bpMainMaterialService.delete(id);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("删除失败", e);
            return ResultBean.fail(101, "删除失败");
        }
    }

    @GetMapping("get")
    public Object getById(Long id) {
        try {
            return ResultBean.success(bpMainMaterialService.getById(id));
        } catch (Exception e) {
            logger.error("查询详情失败", e);
            return ResultBean.fail(101, "查询详情失败:" + e.getMessage());
        }
    }

    @GetMapping("/getCollection")
    public ResultBean getCollection() {
        List<BpDeviceFacilityCollectionBean> collection = bpMainMaterialService.getCollection();
        return ResultBean.wrapSuccessfulResult(collection);
    }

}
